home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl1 / examples / windows / input.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  2.6 KB  |  116 lines

  1. /*
  2.  * Copyright 1996, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /* input.c - open a window and clear the background.
  19.  *    Set up a callback to handle ASCII keyboard input.
  20.  *
  21.  *    SPACE key        - generates a random background color
  22.  *    Escape Key        - exit program
  23.  */
  24. #include <GL/gl.h>
  25. #include <GL/glut.h>
  26.  
  27. #include <stdio.h>
  28. #include <math.h>
  29.  
  30. /* Function Prototypes */
  31.  
  32. GLvoid initgfx( GLvoid );
  33. GLvoid keyboard( GLubyte, GLint, GLint );
  34. GLvoid drawScene( GLvoid );
  35.  
  36. void checkError( char * );
  37. void printHelp( char * );
  38.  
  39. /* Global Definitions */
  40.  
  41. #define KEY_ESC    27    /* ascii value for the escape key */
  42.  
  43. void
  44. main( int argc, char *argv[] )
  45. {
  46.     GLsizei width, height;
  47.  
  48.     glutInit( &argc, argv );
  49.  
  50.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  51.     height = glutGet( GLUT_SCREEN_HEIGHT );
  52.     glutInitWindowPosition( width / 4, height / 4 );
  53.     glutInitWindowSize( width / 2, height / 2 );
  54.     glutInitDisplayMode( GLUT_RGBA );
  55.     glutCreateWindow( argv[0] );
  56.  
  57.     initgfx();
  58.  
  59.     glutKeyboardFunc( keyboard );
  60.     glutDisplayFunc( drawScene ); 
  61.  
  62.     printHelp( argv[0] );
  63.  
  64.     glutMainLoop();
  65. }
  66.  
  67. void
  68. printHelp( char *progname )
  69. {
  70.     fprintf(stdout, 
  71.         "\n%s - demonstrates how to handle keyboard input\n\n"
  72.         "SPACE Key    - generates a random background color\n"
  73.         "Escape Key    - exit the program\n\n",
  74.         progname);
  75. }
  76.  
  77. GLvoid
  78. initgfx( GLvoid )
  79. {
  80.     /* set clear color to magenta */
  81.     glClearColor( 1.0, 0.0, 1.0, 1.0 );
  82. }
  83.  
  84. void 
  85. checkError( char *label )
  86. {
  87.     GLenum error;
  88.     while ( (error = glGetError()) != GL_NO_ERROR )
  89.         printf( "%s: %s\n", label, gluErrorString(error) );
  90. }
  91.  
  92. GLvoid 
  93. keyboard( GLubyte key, GLint x, GLint y )
  94. {
  95.     switch (key) {
  96.     case ' ':    /* SPACE key */
  97.         /* generate a random background color */
  98.         glClearColor( drand48(), drand48(), drand48(), 1.0 ); 
  99.         glutPostRedisplay();
  100.         break;
  101.     case KEY_ESC:    /* Exit when the Escape key is pressed */
  102.         exit(0);
  103.     }
  104. }
  105.  
  106. GLvoid
  107. drawScene( GLvoid )
  108. {
  109.     glClear( GL_COLOR_BUFFER_BIT );
  110.  
  111.     /* Do all your OpenGL rendering here */
  112.  
  113.     checkError( "drawScene" );
  114.     glFlush();
  115. }
  116.